﻿#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
//常用查找算法find_if
//1.内置数据类型
class Greater
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), Greater());
	if (it == v.end())
	{
		cout << "没找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << *it << endl;
	}
}

class Person
{
public:
	Person(string name,int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	string m_Name;
	int m_Age;
};
class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};
//2.查找自定义数据类型
void test02()
{
	vector<Person>v;
	Person p1("AA", 15);
	Person p2("BB", 20);
	Person p3("CC", 25);
	Person p4("DD", 35);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//找一个年龄大于20的
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
	if (it != v.end())
	{
		cout << "找到了" << endl;
		cout << "年龄为" << it->m_Age << "   姓名为:" << it->m_Name << endl;
	}
	else
		cout << "找不到" << endl;
}
int main()
{
	test01();
	test02();
}